In [ ]:
import os
import io
import re
import time
import glob
import requests
import urllib.request
import numpy as np
import pandas as pd
import geopandas as gpd
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
import seaborn as sns
import ipywidgets
In [ ]:
gpd.show_versions()
SYSTEM INFO ----------- python : 3.12.0 | packaged by conda-forge | (main, Oct 3 2023, 08:26:13) [MSC v.1935 64 bit (AMD64)] executable : c:\Users\julia\miniconda3\envs\geo_env\python.exe machine : Windows-11-10.0.22621-SP0 GEOS, GDAL, PROJ INFO --------------------- GEOS : 3.12.0 GEOS lib : None GDAL : 3.7.2 GDAL data dir: None PROJ : 9.3.0 PROJ data dir: C:\Users\julia\miniconda3\envs\geo_env\Library\share\proj PYTHON DEPENDENCIES ------------------- geopandas : 0.14.0 numpy : 1.26.0 pandas : 2.1.2 pyproj : 3.6.1 shapely : 2.0.2 fiona : 1.9.5 geoalchemy2: None geopy : None matplotlib : 3.8.1 mapclassify: 2.6.1 pygeos : None pyogrio : None psycopg2 : None pyarrow : None rtree : 1.1.0
In [ ]:
import warnings
warnings.filterwarnings("ignore")
from pyproj import CRS
Municipality Mapping¶
In [ ]:
ph0m = gpd.GeoDataFrame.from_file('./namria_adm/phl_admbnda_adm2_psa_namria_20200529.shp')
ph0m.head()
Out[ ]:
| Shape_Leng | Shape_Area | ADM2_EN | ADM2_PCODE | ADM2_REF | ADM2ALT1EN | ADM2ALT2EN | ADM1_EN | ADM1_PCODE | ADM0_EN | ADM0_PCODE | date | validOn | validTo | geometry | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 2.640967 | 0.334223 | Abra | PH140100000 | None | None | None | Cordillera Administrative Region | PH140000000 | Philippines (the) | PH | 2016-06-30 | 2020-05-29 | None | POLYGON ((120.96109 17.95348, 120.97201 17.946... |
| 1 | 3.674955 | 0.220065 | Agusan del Norte | PH160200000 | None | None | None | Region XIII | PH160000000 | Philippines (the) | PH | 2016-06-30 | 2020-05-29 | None | MULTIPOLYGON (((125.58886 9.45793, 125.59687 9... |
| 2 | 5.222636 | 0.693968 | Agusan del Sur | PH160300000 | None | None | None | Region XIII | PH160000000 | Philippines (the) | PH | 2016-06-30 | 2020-05-29 | None | POLYGON ((125.88961 8.98195, 125.88896 8.96446... |
| 3 | 4.626091 | 0.139664 | Aklan | PH060400000 | None | None | None | Region VI | PH060000000 | Philippines (the) | PH | 2016-06-30 | 2020-05-29 | None | MULTIPOLYGON (((122.43980 11.59717, 122.43979 ... |
| 4 | 6.507665 | 0.205939 | Albay | PH050500000 | None | None | None | Region V | PH050000000 | Philippines (the) | PH | 2016-06-30 | 2020-05-29 | None | MULTIPOLYGON (((124.20992 13.16871, 124.20993 ... |
In [ ]:
fig, ax = plt.subplots(1, figsize=(15, 20), frameon=True)
ph0m.plot(ax=ax, cmap='Blues', edgecolor='black', k=18, column ='ADM1_EN')
ax.set_title('Regions of the Philippines')
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)
In [ ]:
ph2 = gpd.GeoDataFrame.from_file("namria_adm/phl_admbndp_admALL_psa_namria_itos_20200529.shp")
In [ ]:
ph2
Out[ ]:
| ADM0_EN | ADM0_PCODE | ADM1_EN | ADM1_PCODE | ADM2_EN | ADM2_PCODE | ADM3_EN | ADM3_PCODE | ADM3_REF | ADM3ALT1EN | ADM3ALT2EN | geometry | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | Philippines (the) | PH | Region IV-B | PH170000000 | Palawan | PH175300000 | Aborlan | PH175301000 | None | None | None | POINT (118.44376 9.50866) |
| 1 | Philippines (the) | PH | Region IV-B | PH170000000 | Occidental Mindoro | PH175100000 | Abra de Ilog | PH175101000 | None | None | None | POINT (120.72369 13.39744) |
| 2 | Philippines (the) | PH | Region III | PH030000000 | Bataan | PH030800000 | Abucay | PH030801000 | None | None | None | POINT (120.48722 14.72164) |
| 3 | Philippines (the) | PH | Region II | PH020000000 | Cagayan | PH021500000 | Abulug | PH021501000 | None | None | None | POINT (121.43253 18.39118) |
| 4 | Philippines (the) | PH | Region VIII | PH080000000 | Leyte | PH083700000 | Abuyog | PH083701000 | None | None | None | POINT (125.05031 10.64363) |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 1642 | Philippines (the) | PH | Region IX | PH090000000 | Zamboanga del Sur | PH097300000 | Zamboanga City | PH097332000 | None | None | None | POINT (122.14554 7.07373) |
| 1643 | Philippines (the) | PH | Region VII | PH070000000 | Negros Oriental | PH074600000 | Zamboanguita | PH074625000 | None | None | None | POINT (123.16247 9.16242) |
| 1644 | Philippines (the) | PH | Region III | PH030000000 | Nueva Ecija | PH034900000 | Zaragoza | PH034932000 | None | None | None | POINT (120.79238 15.44499) |
| 1645 | Philippines (the) | PH | Region VI | PH060000000 | Iloilo | PH063000000 | Zarraga | PH063047000 | None | None | None | POINT (122.62828 10.82734) |
| 1646 | Philippines (the) | PH | Region VIII | PH080000000 | Samar | PH086000000 | Zumarraga | PH086023000 | None | None | None | POINT (124.85655 11.66077) |
1647 rows × 12 columns
In [ ]:
fig, ax = plt.subplots(1, figsize=(15, 20), frameon=True)
ph0m.plot(ax=ax, cmap='Blues', edgecolor='black', k=18, column ='ADM1_EN')
ax.set_title('Regions of the Philippines')
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)
ph2.plot(ax=ax, edgecolor='#FFFFFF', cmap='Greens', k = 18)#,column='gid')
ax.set_title('Municipalities of the Philippines', fontdict={'fontsize': '25', 'fontweight' : '3'}, loc='center')
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.axis('off')
Out[ ]:
(113.66153858585, 127.22131926315006, 3.760564947900073, 21.948615028100072)
Class Exercise 1:¶
- Recreate the map and put the names of each province and municipality.
- Using the dataframe below, generate a map of barangays.
- Open GADM 1,2,3 and NAMRIA 1,2,3 and create basic maps.
In [ ]:
gadm3 = gpd.GeoDataFrame.from_file(".\GADM\gadm41_PHL_3.shp")
gadm3
Out[ ]:
| GID_3 | GID_0 | COUNTRY | GID_1 | NAME_1 | NL_NAME_1 | GID_2 | NAME_2 | NL_NAME_2 | NAME_3 | VARNAME_3 | NL_NAME_3 | TYPE_3 | ENGTYPE_3 | CC_3 | HASC_3 | geometry | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | PHL.1.1.1_1 | PHL | Philippines | PHL.1_1 | Abra | NA | PHL.1.1_1 | Bangued | NA | Agtangao | NA | NA | Barangay | Village | NA | NA | POLYGON ((120.62390 17.57298, 120.62534 17.573... |
| 1 | PHL.1.1.2_1 | PHL | Philippines | PHL.1_1 | Abra | NA | PHL.1.1_1 | Bangued | NA | Angad | NA | NA | Barangay | Village | NA | NA | POLYGON ((120.63432 17.58044, 120.63396 17.579... |
| 2 | PHL.1.1.3_1 | PHL | Philippines | PHL.1_1 | Abra | NA | PHL.1.1_1 | Bangued | NA | Bañacao | NA | NA | Barangay | Village | NA | NA | POLYGON ((120.59626 17.61632, 120.59573 17.605... |
| 3 | PHL.1.1.4_1 | PHL | Philippines | PHL.1_1 | Abra | NA | PHL.1.1_1 | Bangued | NA | Bangbangar | NA | NA | Barangay | Village | NA | NA | POLYGON ((120.59573 17.60569, 120.59626 17.616... |
| 4 | PHL.1.1.5_1 | PHL | Philippines | PHL.1_1 | Abra | NA | PHL.1.1_1 | Bangued | NA | Cabuloan | NA | NA | Barangay | Village | NA | NA | POLYGON ((120.58296 17.59962, 120.59573 17.605... |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 41943 | PHL.81.16.21_1 | PHL | Philippines | PHL.81_1 | Zamboanga Sibugay | NA | PHL.81.16_1 | Tungawan | NA | Tigbanuang | NA | NA | Barangay | Village | NA | NA | POLYGON ((122.26558 7.60569, 122.27217 7.61987... |
| 41944 | PHL.81.16.22_1 | PHL | Philippines | PHL.81_1 | Zamboanga Sibugay | NA | PHL.81.16_1 | Tungawan | NA | Tigbucay | NA | NA | Barangay | Village | NA | NA | MULTIPOLYGON (((122.43819 7.52123, 122.43851 7... |
| 41945 | PHL.81.16.23_1 | PHL | Philippines | PHL.81_1 | Zamboanga Sibugay | NA | PHL.81.16_1 | Tungawan | NA | Tigpalay | NA | NA | Barangay | Village | NA | NA | POLYGON ((122.22511 7.50056, 122.22805 7.51891... |
| 41946 | PHL.81.16.24_1 | PHL | Philippines | PHL.81_1 | Zamboanga Sibugay | NA | PHL.81.16_1 | Tungawan | NA | Timbabauan | NA | NA | Barangay | Village | NA | NA | POLYGON ((122.41174 7.62762, 122.42164 7.62130... |
| 41947 | PHL.81.16.25_1 | PHL | Philippines | PHL.81_1 | Zamboanga Sibugay | NA | PHL.81.16_1 | Tungawan | NA | Upper Tungawan | NA | NA | Barangay | Village | NA | NA | POLYGON ((122.25346 7.57717, 122.26107 7.59401... |
41948 rows × 17 columns
In [ ]:
barangay = gadm3.loc[gadm3['NAME_2']=='Quezon City']
barangay
Out[ ]:
| GID_3 | GID_0 | COUNTRY | GID_1 | NAME_1 | NL_NAME_1 | GID_2 | NAME_2 | NL_NAME_2 | NAME_3 | VARNAME_3 | NL_NAME_3 | TYPE_3 | ENGTYPE_3 | CC_3 | HASC_3 | geometry | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 25834 | PHL.47.14.1_1 | PHL | Philippines | PHL.47_1 | Metropolitan Manila | NA | PHL.47.14_1 | Quezon City | NA | Alicia | NA | NA | Barangay | Village | NA | NA | POLYGON ((121.01987 14.66056, 121.02048 14.660... |
| 25835 | PHL.47.14.2_1 | PHL | Philippines | PHL.47_1 | Metropolitan Manila | NA | PHL.47.14_1 | Quezon City | NA | Amihan | NA | NA | Barangay | Village | NA | NA | POLYGON ((121.06464 14.63194, 121.06451 14.632... |
| 25836 | PHL.47.14.3_1 | PHL | Philippines | PHL.47_1 | Metropolitan Manila | NA | PHL.47.14_1 | Quezon City | NA | Apolonio Samson | NA | NA | Barangay | Village | NA | NA | POLYGON ((121.00325 14.66466, 121.00336 14.664... |
| 25837 | PHL.47.14.4_1 | PHL | Philippines | PHL.47_1 | Metropolitan Manila | NA | PHL.47.14_1 | Quezon City | NA | Aurora | NA | NA | Barangay | Village | NA | NA | POLYGON ((121.00472 14.61461, 121.00469 14.615... |
| 25838 | PHL.47.14.5_1 | PHL | Philippines | PHL.47_1 | Metropolitan Manila | NA | PHL.47.14_1 | Quezon City | NA | Baesa | NA | NA | Barangay | Village | NA | NA | POLYGON ((121.00394 14.67059, 121.00516 14.670... |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 25970 | PHL.47.14.137_1 | PHL | Philippines | PHL.47_1 | Metropolitan Manila | NA | PHL.47.14_1 | Quezon City | NA | Veterans Village | NA | NA | Barangay | Village | NA | NA | POLYGON ((121.02706 14.65489, 121.02672 14.653... |
| 25971 | PHL.47.14.138_1 | PHL | Philippines | PHL.47_1 | Metropolitan Manila | NA | PHL.47.14_1 | Quezon City | NA | Villa Maria Clara | NA | NA | Barangay | Village | NA | NA | POLYGON ((121.06843 14.61768, 121.06782 14.616... |
| 25972 | PHL.47.14.139_1 | PHL | Philippines | PHL.47_1 | Metropolitan Manila | NA | PHL.47.14_1 | Quezon City | NA | West Kamias | NA | NA | Barangay | Village | NA | NA | POLYGON ((121.04921 14.62949, 121.04906 14.629... |
| 25973 | PHL.47.14.140_1 | PHL | Philippines | PHL.47_1 | Metropolitan Manila | NA | PHL.47.14_1 | Quezon City | NA | West Triangle | NA | NA | Barangay | Village | NA | NA | POLYGON ((121.03325 14.64692, 121.03328 14.646... |
| 25974 | PHL.47.14.141_1 | PHL | Philippines | PHL.47_1 | Metropolitan Manila | NA | PHL.47.14_1 | Quezon City | NA | White Plains | NA | NA | Barangay | Village | NA | NA | POLYGON ((121.07329 14.61108, 121.07337 14.610... |
141 rows × 17 columns
In [ ]:
fig, ax = plt.subplots(1, figsize=(20, 27), frameon=True)
ph0m.plot(ax=ax, cmap='Blues', edgecolor='black', k=18, column ='ADM1_EN')
ax.set_title('Province of the Philippines')
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)
dissolved_provinces = gadm3.dissolve(by='NAME_1')
# Add labels to each dissolved province
for x, y, label in zip(dissolved_provinces.geometry.centroid.x, dissolved_provinces.geometry.centroid.y, dissolved_provinces.index):
ax.text(x, y, label, fontsize=8, ha='center')
In [ ]:
fig, ax = plt.subplots(1, figsize=(20, 26), frameon=True)
barangay.plot(ax=ax, cmap='Blues', edgecolor='black', k=18, column ='NAME_1')
ax.set_title('Maps of Barangays in Quezon City')
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)
# barangay = gadm3.dissolve(by='NAME_1')
# Add labels to each dissolved province
for x, y, label in zip(barangay.geometry.centroid.x, barangay.geometry.centroid.y, barangay['NAME_3']):
ax.text(x, y, label, fontsize=8, ha='center')
In [ ]:
gadm2 = gpd.GeoDataFrame.from_file(".\GADM\gadm41_PHL_2.shp")
gadm2
Out[ ]:
| GID_2 | GID_0 | COUNTRY | GID_1 | NAME_1 | NL_NAME_1 | NAME_2 | VARNAME_2 | NL_NAME_2 | TYPE_2 | ENGTYPE_2 | CC_2 | HASC_2 | geometry | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | PHL.1.1_1 | PHL | Philippines | PHL.1_1 | Abra | NA | Bangued | NA | NA | Bayan|Munisipyo | Municipality | 140101 | PH.AB.BN | POLYGON ((120.61134 17.49558, 120.60941 17.496... |
| 1 | PHL.1.2_1 | PHL | Philippines | PHL.1_1 | Abra | NA | Boliney | NA | NA | Bayan|Munisipyo | Municipality | 140102 | PH.AB.BL | POLYGON ((120.81400 17.34210, 120.76862 17.340... |
| 2 | PHL.1.3_1 | PHL | Philippines | PHL.1_1 | Abra | NA | Bucay | NA | NA | Bayan|Munisipyo | Municipality | 140103 | PH.AB.BU | POLYGON ((120.67766 17.45736, 120.67324 17.469... |
| 3 | PHL.1.4_1 | PHL | Philippines | PHL.1_1 | Abra | NA | Bucloc | NA | NA | Bayan|Munisipyo | Municipality | 140104 | PH.AB.BC | POLYGON ((120.78922 17.44525, 120.80331 17.449... |
| 4 | PHL.1.5_1 | PHL | Philippines | PHL.1_1 | Abra | NA | Daguioman | NA | NA | Bayan|Munisipyo | Municipality | 140105 | PH.AB.DG | POLYGON ((120.90749 17.41933, 120.89281 17.446... |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 1642 | PHL.81.12_1 | PHL | Philippines | PHL.81_1 | Zamboanga Sibugay | NA | Roseller Lim | NA | NA | Bayan|Munisipyo | Municipality | 98312 | PH.ZS.RS | MULTIPOLYGON (((122.47417 7.63000, 122.47417 7... |
| 1643 | PHL.81.13_1 | PHL | Philippines | PHL.81_1 | Zamboanga Sibugay | NA | Siay | NA | NA | Bayan|Munisipyo | Municipality | 98313 | PH.ZS.SA | MULTIPOLYGON (((122.86974 7.65476, 122.86914 7... |
| 1644 | PHL.81.14_1 | PHL | Philippines | PHL.81_1 | Zamboanga Sibugay | NA | Talusan | NA | NA | Bayan|Munisipyo | Municipality | 98314 | PH.ZS.TL | MULTIPOLYGON (((122.89452 7.35639, 122.90918 7... |
| 1645 | PHL.81.15_1 | PHL | Philippines | PHL.81_1 | Zamboanga Sibugay | NA | Titay | NA | NA | Bayan|Munisipyo | Municipality | 98315 | PH.ZS.TT | POLYGON ((122.48864 7.76403, 122.48551 7.77061... |
| 1646 | PHL.81.16_1 | PHL | Philippines | PHL.81_1 | Zamboanga Sibugay | NA | Tungawan | NA | NA | Bayan|Munisipyo | Municipality | 98316 | PH.ZS.TN | MULTIPOLYGON (((122.23621 7.54273, 122.24481 7... |
1647 rows × 14 columns
In [ ]:
fig, ax = plt.subplots(1, figsize=(20, 26), frameon=True)
gadm2.plot(ax=ax, cmap='Blues', edgecolor='black', k=18, column ='NAME_1')
ax.set_title('Municipalities of the Philippines')
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)
In [ ]:
gadm1 = gpd.GeoDataFrame.from_file(".\GADM\gadm41_PHL_1.shp")
gadm1
Out[ ]:
| GID_1 | GID_0 | COUNTRY | NAME_1 | VARNAME_1 | NL_NAME_1 | TYPE_1 | ENGTYPE_1 | CC_1 | HASC_1 | ISO_1 | geometry | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | PHL.1_1 | PHL | Philippines | Abra | NA | NA | Lalawigan | Province | 1401 | PH.AB | PH-ABR | POLYGON ((120.74773 17.15755, 120.73747 17.156... |
| 1 | PHL.2_1 | PHL | Philippines | Agusan del Norte | NA | NA | Lalawigan | Province | 1602 | PH.AN | PH-AGN | POLYGON ((125.51141 8.70021, 125.49689 8.70089... |
| 2 | PHL.3_1 | PHL | Philippines | Agusan del Sur | NA | NA | Lalawigan | Province | 1603 | PH.AS | PH-AGS | POLYGON ((126.05209 7.93625, 126.04572 7.93504... |
| 3 | PHL.4_1 | PHL | Philippines | Aklan | NA | NA | Lalawigan | Province | 604 | PH.AK | PH-AKL | MULTIPOLYGON (((122.28702 11.74581, 122.28805 ... |
| 4 | PHL.5_1 | PHL | Philippines | Albay | NA | NA | Lalawigan | Province | 505 | PH.AL | PH-ALB | MULTIPOLYGON (((123.44640 13.02776, 123.44541 ... |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 76 | PHL.77_1 | PHL | Philippines | Tawi-Tawi | NA | NA | Lalawigan | Province | 1570 | PH.TT | PH-TAW | MULTIPOLYGON (((119.93195 4.89917, 119.93195 4... |
| 77 | PHL.78_1 | PHL | Philippines | Zambales | NA | NA | Lalawigan | Province | 155 | PH.PN | PH-ZMB | MULTIPOLYGON (((120.29529 14.80142, 120.29581 ... |
| 78 | PHL.79_1 | PHL | Philippines | Zamboanga del Norte | NA | NA | Lalawigan | Province | 972 | PH.ZN | PH-ZAN | MULTIPOLYGON (((121.90389 7.15028, 121.90417 7... |
| 79 | PHL.80_1 | PHL | Philippines | Zamboanga del Sur | NA | NA | Lalawigan | Province | 973 | PH.ZS | PH-ZAS | MULTIPOLYGON (((122.06706 6.86892, 122.06729 6... |
| 80 | PHL.81_1 | PHL | Philippines | Zamboanga Sibugay | NA | NA | Lalawigan | Province | 983 | PH.ZY | PH-ZSI | MULTIPOLYGON (((122.85913 7.27263, 122.85863 7... |
81 rows × 12 columns
In [ ]:
fig, ax = plt.subplots(1, figsize=(20, 26), frameon=True)
gadm1.plot(ax=ax, cmap='Blues', edgecolor='black', k=18, column ='NAME_1')
ax.set_title('Regions of the Philippines')
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)
dissolved_provinces = gadm1.dissolve(by='NAME_1')
# Add labels to each dissolved province
for x, y, label in zip(dissolved_provinces.geometry.centroid.x, dissolved_provinces.geometry.centroid.y, dissolved_provinces.index):
ax.text(x, y, label, fontsize=8, ha='center')
Barangays in Manila¶
In [ ]:
fig, ax = plt.subplots(1, figsize=(20, 26), frameon=True)
barangay = gadm3.loc[gadm3['NAME_2']=='Manila']
barangay.plot(ax=ax, cmap='Blues', edgecolor='black', k=18, column ='NAME_1')
ax.set_title('Barangays in Manila')
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)
barangay['NAME_3'] = barangay['NAME_3'].str.replace('Barangay','')
# barangay = gadm3.dissolve(by='NAME_1')
# Add labels to each dissolved province
for x, y, label in zip(barangay.geometry.centroid.x, barangay.geometry.centroid.y, barangay['NAME_3']):
ax.text(x, y, label, fontsize=8, ha='center')
Folium¶
Folium is a powerful Python library that helps you create several types of Leaflet maps. By default, Folium creates a map in a separate HTML file. Since Folium results are interactive, this library is very useful for dashboard building. You can also create inline Jupyter maps in Folium.
For reference: https://realpython.com/python-folium-web-maps-from-data/#:~:text=Python's%20Folium%20library%20gives%20you,can%20share%20as%20a%20website.
In [ ]:
import folium
# Create a map centered on the Philippines
m = folium.Map(location=[12.8797, 121.7740], zoom_start=5)
# Display the map
m.save("philippines_map.html")
In [ ]:
#Folium with point/centers
map_philippines = folium.Map(location=[12.8797, 121.7740], zoom_start=6)
# Add markers for regional locations, main malls, convenient stores, and train stations
locations = {
"Manila": [14.5995, 120.9842],
"Cebu": [10.3157, 123.8854],
"Davao": [7.1907, 125.4553],
"Baguio": [16.4023, 120.5960],
"Iloilo": [10.7202, 122.5621],
"SM Mall of Asia": [14.5346, 120.9828], # Example: SM Mall of Asia in Manila
"Ayala Center Cebu": [10.3187, 123.9056], # Example: Ayala Center Cebu
"7-Eleven Manila": [14.5724, 121.0359], # Example: 7-Eleven in Manila
"Mini Stop Cebu": [10.3191, 123.9055], # Example: Mini Stop in Cebu
"Manila Railway Station": [14.5942, 120.9795], # Example: Train station in Manila
"Cebu South Road Properties Station": [10.2928, 123.8931], # Example: Train station in Cebu
}
for location, coordinates in locations.items():
folium.Marker(location=coordinates, popup=location).add_to(map_philippines)
# Save the map to an HTML file
map_philippines.save("philippines_map_with_train_locations.html")
In [ ]:
# Create a map centered at the approximate center of the Philippines
map_philippines = folium.Map(location=[12.8797, 121.7740], zoom_start=6)
# Manila
manila_popup_content = """
<h3>Manila</h3>
<p>The capital city of the Philippines.</p>
<img src="https://media.istockphoto.com/id/1092423872/photo/skyline-of-manila-city-and-manila-bay-philippines.jpg?s=2048x2048&w=is&k=20&c=Kin2o9EjTQp8AqLBSZGNzq3ui9_Ghv6rjz7jzsubpdY=" width="200px">
"""
folium.Marker(location=[14.5995, 120.9842], popup=folium.Popup(manila_popup_content, max_width=300)).add_to(map_philippines)
# Cebu
cebu_popup_content = """
<h3>Cebu</h3>
<p>A major city in the Visayas region.</p>
<img src="cebu_image.jpg" width="200px">
"""
folium.Marker(location=[10.3157, 123.8854], popup=folium.Popup(cebu_popup_content, max_width=300)).add_to(map_philippines)
# Davao
davao_popup_content = """
<h3>Davao</h3>
<p>The largest city on the island of Mindanao.</p>
<img src="davao_image.jpg" width="200px">
"""
folium.Marker(location=[7.1907, 125.4553], popup=folium.Popup(davao_popup_content, max_width=300)).add_to(map_philippines)
# Display the map in the Jupyter Notebook
map_philippines
Out[ ]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Class Exercise 2:¶
- Find a PH dataset and map regional or provincial data using folium
- [Bonus] Try to improve the folium mapps.
Choropleth map, Population Density of Cities in NCR (2015)¶
In [ ]:
import base64
pop_density = pd.read_csv("NCR pop.csv",encoding='latin-1')
political_countries_url = (
"https://raw.githubusercontent.com/macoymejia/geojsonph/master/Philippines/Luzon/Metropolitant%20Manila/MetropolitantManila.json"
)
m = folium.Map(location=(14.6, 121), zoom_start=11.5, tiles="cartodb positron")
encoded = base64.b64encode(open("pexels-kelly-19063352.jpg", 'rb').read()).decode()
from folium import IFrame
html = '<h3 style="font-family: calibri">Manila</h3><p style="font-family: calibri">The most populated city in NCR. (2015)</p><img src="data:image/png;base64,{}">'.format
iframe = IFrame(html(encoded), width=350, height=300)
popup = folium.Popup(iframe, max_width=300)
# manila_popup_content = """
# <h3>Manila</h3>
# <p>The capital city of the Philippines.</p>
# <img src="data:image/jpg;base64,{}" width="200px",>
# """
kw = {"prefix": "fa", "color": "black", "icon": "arrow-up"}
angle = 180
icon = folium.Icon(angle=angle, **kw)
folium.Marker([14.5995, 120.9842], popup=popup, icon=icon, tooltip=str("Manila")).add_to(m)
folium.Choropleth(
geo_data=political_countries_url,
data=pop_density,
columns=["NAME_2", "population_density"],
key_on="feature.properties.NAME_2",
fill_color="RdYlGn_r",
fill_opacity=0.5,
line_opacity=0.3,
nan_fill_color="white",
legend_name="Population Density",
Highlight= True
).add_to(m)
folium.LayerControl(collapsed=False).add_to(m)
m
Out[ ]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [ ]: